/**
 * Copyright (c) 2010 David Hamrick, Guy Kopsombut, Emily Ross 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "Passenger.h"
#import "SBJSON.h"
#import "ServerComm.h"

@implementation Passenger

@synthesize dict,statusNum;

- (id)init
{
	if(self = [super init])
	{
		self.dict = [[NSMutableDictionary alloc] init];
	}
	return self;
}

+ (Passenger*)PassengerWithDict:(NSDictionary*)dictionary
{
	Passenger* p = [[Passenger alloc] init];
	p.dict = [dictionary retain];
	return p;
}

+ (Passenger*)PassengerWithId:(NSInteger)ID
{
	SBJSON* json = [[SBJSON alloc] init];
	NSString* str = [ServerComm sendRequestWithURL:[NSString stringWithFormat:HOSTNAME"passengers/%d",ID] method:@"GET" body:@""];
	return [Passenger PassengerWithDict:[json objectWithString:str]];
}

+ (Passenger*)PassengerWithAssignedCabID:(NSString*)cabID
{
	NSArray* passengerList = [Passenger allPassengers];
	for(Passenger* p in passengerList)
	{
		if([[p cabid] isEqualToString:cabID])
			return p;
	}
	return nil;
}

+ (void)deleteAll
{
	[ServerComm sendRequestWithURL:HOSTNAME"passengers" method:@"DELETE" body:@""];
}


+ (NSArray*)allPassengers
{
	SBJSON* json = [[SBJSON alloc] init];
	NSString* str = [ServerComm sendRequestWithURL:HOSTNAME@"passengers" method:@"GET" body:@""];
	NSMutableArray* passengerList = [[[NSMutableArray alloc] init] autorelease];
	NSArray* passengerDictArray = [json objectWithString:str];
	for(NSDictionary* dict in passengerDictArray)
		[passengerList addObject:[Passenger PassengerWithDict:dict]];
	return passengerList;
}

- (void)save
{
	SBJSON* json = [[SBJSON alloc] init];
	NSString* jsonObj = [json stringWithObject:dict];
	NSString* str;
	if([self ID] == 0)
		str = [ServerComm sendRequestWithURL:HOSTNAME"passengers" method:@"PUT" body:jsonObj];
	else
		str = [ServerComm sendRequestWithURL:[NSString stringWithFormat:HOSTNAME"passengers/%d",[self ID]] method:@"POST" body:jsonObj];
	self.dict = [[json objectWithString:str] retain];
}

- (void)delete
{
	[ServerComm sendRequestWithURL:[NSString stringWithFormat:HOSTNAME"passengers/%d",[self ID]] method:@"DELETE" body:nil];
}

- (void)setDict:(NSMutableDictionary *)newDict
{
	dict = newDict;
}

- (NSInteger)count
{
	return [dict count];
}

- (void)setObject:(NSObject*)object forKey:(NSString*)key
{
	[dict setObject:object forKey:key];
}

- (id)objectForKey:(NSString*)key
{
	return [dict objectForKey:key];
}

- (NSString*)name
{
	return [dict objectForKey:@"name"];
}

- (void)setName:(NSString*)name
{
	[dict setObject:name forKey:@"name"];
}

- (NSInteger)ID
{
	return [[dict objectForKey:@"id"] integerValue];
}

- (NSString*)phone
{
	return [dict objectForKey:@"phone"];
}

- (void)setPhone:(NSString*)phone
{
	[dict setObject:phone forKey:@"phone"];
}

- (void)setAddress:(NSString*)address
{
	[dict setObject:address forKey:@"address"];
}

- (NSString*)address
{
	return [dict objectForKey:@"address"];
}

- (NSString*)cabid
{
	return [dict objectForKey:@"cabid"];
}

- (void)setCabId:(NSString*)cabId
{
	[dict setObject:cabId forKey:@"cabid"];
}


- (NSString*)status
{
	return [dict objectForKey:@"status"];
}

- (void)setStatus:(NSString*)status
{
	[dict setObject:status forKey:@"status"];
}


@end
